package cn.itcast.service.impl;

import cn.itcast.dao.AccountDao;
import cn.itcast.domain.Account;
import cn.itcast.service.AccountService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class AccountServiceImpl implements AccountService {
    @Autowired
    private AccountDao accountDao;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<Account> findAll() throws Exception {
            List<Account> list = accountDao.findAll();
            return list;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Account findById(Integer id) throws Exception {
        return accountDao.findById(id);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void update(Account account) throws Exception {
        accountDao.update(account);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void delete(Integer id) throws Exception {
        accountDao.delete(id);

    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void add(Account account) throws Exception {
        accountDao.add(account);

    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public PageInfo<Account> pageQuery(Integer pageNum, Integer pageSize) throws Exception {
        PageHelper.startPage(pageNum,pageSize);
        List<Account> list = accountDao.findAll();
        PageInfo<Account> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
}
